home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 201-225 / disk_214 / mandelvroom / src / cycle.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  13KB  |  579 lines

  1. /*
  2.  * MandelVroom 2.0
  3.  *
  4.  * (c) Copyright 1987,1989  Kevin L. Clague, San Jose, CA
  5.  *
  6.  * All rights reserved.
  7.  *
  8.  * Permission is hereby granted to distribute this program's source
  9.  * executable, and documentation for non-comercial purposes, so long as the
  10.  * copyright notices are not removed from the sources, executable or
  11.  * documentation.  This program may not be distributed for a profit without
  12.  * the express written consent of the author Kevin L. Clague.
  13.  *
  14.  * This program is not in the public domain.
  15.  *
  16.  * Fred Fish is expressly granted permission to distribute this program's
  17.  * source and executable as part of the "Fred Fish freely redistributable
  18.  * Amiga software library."
  19.  *
  20.  * Permission is expressly granted for this program and it's source to be
  21.  * distributed as part of the Amicus Amiga software disks, and the
  22.  * First Amiga User Group's Hot Mix disks.
  23.  *
  24.  * contents:  this file contains the code to open and close the color
  25.  * cycling tool.  It also contains the functions that implement the color
  26.  * cycling commands.
  27.  */
  28.  
  29. #include "mandp.h"
  30. #include <ctype.h>
  31.  
  32. #define PENLEFT 8
  33. #define PENTOP  15
  34.  
  35. UBYTE CycleOpen;
  36.  
  37. /*
  38.  * Holder for Allocated color potentiometer gadgets.
  39.  */
  40. struct Gadget *SpeedPot;
  41. struct Gadget *DirGadget;
  42. struct Gadget *CycleOnGadget;
  43. struct Gadget *CycleOffGadget;
  44.  
  45. struct Window          *CycWind;
  46.  
  47. struct NewWindow NewCyc = {
  48.    128,70,                   /* start position           */
  49.    164,80,                   /* width, height            */
  50.    (UBYTE) 0, (UBYTE) -1,    /* detail pen, block pen    */
  51.    NULL,                     /* IDCMP flags */
  52.                              /* MandWind flags */
  53.    WINDOWCLOSE   | WINDOWDRAG | WINDOWDEPTH | NOCAREREFRESH | SMART_REFRESH,
  54.    (struct Gadget *) NULL,   /* first gadget             */
  55.    (struct Image *) NULL,    /* user checkmark           */
  56.    (UBYTE *) "Cycling",      /* window title             */
  57.    (struct Screen *) NULL,   /* pointer to screen        */
  58.    (struct BitMap *) NULL,   /* pointer to superbitmap   */
  59.    80,80,80,80,              /* sizing                   */
  60.    CUSTOMSCREEN              /* type of screen           */
  61.    };
  62.  
  63. int EditRange;
  64. int RangeFirstPen;
  65.  
  66. SlideSpeedCmd(Msg)
  67.   struct IntuiMessage *Msg;
  68. {
  69.   struct Gadget *gadget;
  70.  
  71.   gadget = (struct Gadget *) Msg->IAddress;
  72.  
  73.   switch( Msg->Class ) {
  74.  
  75.     case GADGETDOWN:                          /* Start Speed slide    */
  76.          if (CurPict->CycleOn == 0)
  77.            CreateCycle();
  78.          ModifyIDCMP(CycWind,(long) CycWind->IDCMPFlags | MOUSEMOVE);
  79.          State = SLIDESPEEDSTATE;
  80.          break;
  81.  
  82.     case MOUSEMOVE:                           /* change the speed */
  83.          SetSpeed();
  84.          break;
  85.  
  86.     case GADGETUP:                            /* Stop Speed slide */
  87.          if (CurPict->CycleOn == 0) {
  88.            KillCycle();
  89.            LoadRGB4(vp, CurPict->RGBs, Num_vp_Colors);
  90.          }
  91.          SetSpeed();
  92.          ModifyIDCMP(CycWind,(long) CycWind->IDCMPFlags & ~MOUSEMOVE);
  93.          State = IDLESTATE;
  94.          break;
  95.   }
  96. }
  97.  
  98. CycleRangeCmd(Msg)
  99.   struct IntuiMessage *Msg;
  100. {
  101.   struct Gadget  *gadget;
  102.   int id;
  103.  
  104.   /* Need contour selection to complete */
  105.  
  106.   gadget = (struct Gadget *) Msg->IAddress;
  107.   id = gadget->GadgetID;
  108.  
  109.   if (Msg->Class == GADGETDOWN) {
  110.     if (id == CYCRANGE) {
  111.  
  112.       StopCycle();
  113.  
  114.       /* Color Palette command picks up the finish of the range cmd*/
  115.  
  116.       if (CurPen < Num_vp_Colors) {
  117.         SetToPointer();
  118.         RangeFirstPen = CurPen;
  119.         State = CYCLERANGESTATE;
  120.       } else {
  121.         DispErrMsg("Can't set color cycle with hafbrite pens",0);
  122.       }
  123.     } else
  124.     if (WIND_TYPE(id) == PALTYPE && GADG_TYPE(id) == PALPENS) {
  125.  
  126.       FinishRangeCmd(GADG_NUM(id));
  127.       State = IDLESTATE;
  128.     }
  129.   }
  130. }
  131.  
  132. ToggleDirCmd(Msg)
  133.   struct IntuiMessage *Msg;
  134. {
  135.   struct Gadget *gadget = (struct Gadget *) Msg->IAddress;
  136.  
  137.   StopCycle();
  138.  
  139.   /* Set Direction into current cycle info structure */
  140.  
  141.   if (gadget->Flags & SELECTED) {
  142.     CurPict->Crngs[ EditRange ].active &= ~REVERSE;
  143.   } else {
  144.     CurPict->Crngs[ EditRange ].active |= REVERSE;
  145.   }
  146.   State = IDLESTATE;
  147. }
  148.  
  149. CycleOnOffCmd(Msg)
  150.   struct IntuiMessage *Msg;
  151. {
  152.   if (CurPict == NULL)
  153.     return;
  154.  
  155.   if (CurPict->CycleOn) {
  156.  
  157.     StopCycle();
  158.   } else {
  159.  
  160.     CreateCycle();
  161.     CurPict->CycleOn = 1;
  162.     SetOnString(1);
  163.   }
  164.   State = IDLESTATE;
  165. }
  166.  
  167. StopCycle()
  168. {
  169.   if (CurPict->CycleOn == 1) {
  170.     KillCycle();
  171.     LoadRGB4(vp, CurPict->RGBs, Num_vp_Colors);
  172.     CurPict->CycleOn = 0;
  173.     SetOnString( 0 );
  174.   }
  175. }
  176.  
  177. SelRangeCmd(Msg)
  178.   struct IntuiMessage *Msg;
  179. {
  180.   struct Gadget *gadget = (struct Gadget *) Msg->IAddress;
  181.   int    id = gadget->GadgetID;
  182.   int    newpen;
  183.   static int end;
  184.  
  185.   if (WIND_TYPE(id) == CYCTYPE && GADG_TYPE(id) == CYCRNUMS) {
  186.  
  187.     EditRange = GADG_NUM(id);
  188.  
  189.     StopCycle();
  190.  
  191.     /* move current pen to this range's first pen */
  192.  
  193.     BoxPen( CurPen, NORMALPEN );
  194.  
  195.     /* toggle endpoint to display */
  196.  
  197.     if (end ^= 1) {
  198.       CurPen = CurPict->Crngs[ EditRange ].low;
  199.     } else {
  200.       CurPen = CurPict->Crngs[ EditRange ].high;
  201.     }
  202.  
  203.     BoxPen( CurPen, MEDIUMPEN );
  204.  
  205.     ModifySpeedPot();
  206.  
  207.     SetDirGadget( CurPict );
  208.  
  209.     ExcludeGadgets( gadget );
  210.   }
  211. }
  212.  
  213. SetDirGadget( Pict )
  214.   struct Picture *Pict;
  215. {
  216.   int place;
  217.  
  218.   if (Pict) {
  219.  
  220.     if (CycWind) {
  221.  
  222.       place = RemoveGadget(CycWind,DirGadget);
  223.  
  224.       if (Pict->Crngs[EditRange].active & REVERSE) {
  225.         DirGadget->Flags &= ~SELECTED;
  226.       } else {
  227.         DirGadget->Flags |=  SELECTED;
  228.       }
  229.       AddGadget( CycWind, DirGadget, place);
  230.       RefreshGList( DirGadget, CycWind, NULL, 1);
  231.     }
  232.   }
  233. }
  234.  
  235. ModifySpeedPot()
  236. {
  237.   int place;
  238.  
  239.   if (CycWind) {
  240.     /* ModifyProp to match EditRange's CycleSpeed */
  241.  
  242.     place = RemoveGadget(CycWind,SpeedPot);
  243.  
  244.     ((struct PropInfo *) SpeedPot->SpecialInfo)->HorizPot =
  245.       CurPict->Crngs[ EditRange ].rate << 1;
  246.  
  247.     AddGadget( CycWind, SpeedPot, place);
  248.     RefreshGList( SpeedPot, CycWind, NULL, 1);
  249.   }
  250. }
  251.  
  252. ExcludeGadgets( gadget )
  253.   register struct Gadget *gadget;
  254. {
  255.   register struct Gadget *NextGadget;
  256.   register place;
  257.  
  258.   NextGadget = gadget;
  259.  
  260.   do {
  261.  
  262.     place = RemoveGadget( CycWind, NextGadget );
  263.  
  264.     if (NextGadget == gadget) {
  265.       NextGadget->Flags |=  SELECTED;
  266.     } else {
  267.       NextGadget->Flags &= ~SELECTED;
  268.     }
  269.  
  270.     AddGadget( CycWind, NextGadget, place );
  271.     RefreshGList( NextGadget, CycWind, NULL, 1);
  272.  
  273.     NextGadget = (struct Gadget *) NextGadget->UserData;
  274.  
  275.   } while ( NextGadget && NextGadget != gadget );
  276. }
  277.  
  278. static struct PropInfo *PropInfo;
  279.  
  280. SetSpeed()
  281. {
  282.   extern LONG cyClocks[];
  283.  
  284.   CurPict->Crngs[EditRange].rate = PropInfo->HorizPot >> 1;
  285.   cyClocks[EditRange] = 0;
  286. }
  287.  
  288. FinishRangeCmd(pen)
  289.   int pen;
  290. {
  291.   int t;
  292.  
  293.   if (CurPict == NULL)
  294.     return;
  295.  
  296.   t = Num_vp_Colors - 1;
  297.  
  298.   if (RangeFirstPen > t || pen > t)
  299.     return;
  300.  
  301.   if (RangeFirstPen > pen) {
  302.  
  303.     t = pen;
  304.     pen = RangeFirstPen;
  305.     RangeFirstPen = t;
  306.  
  307.     CurPict->Crngs[EditRange].active |= REVERSE;
  308.  
  309.   } else {
  310.  
  311.     CurPict->Crngs[EditRange].active &= ~REVERSE;
  312.   }
  313.  
  314.   SetDirGadget( CurPict );
  315.  
  316.   CurPict->Crngs[EditRange].low  = RangeFirstPen;
  317.   CurPict->Crngs[EditRange].high = pen;
  318.  
  319.   if (pen == RangeFirstPen) {
  320.     CurPict->Crngs[EditRange].active &= ~1;
  321.   } else {
  322.     CurPict->Crngs[EditRange].active |=  1;
  323.   }
  324. }
  325.  
  326. SetOnString( OnOff )
  327. {
  328.   int place;
  329.   register struct Gadget *SrcGadget,*DstGadget;
  330.  
  331.   if (CycWind == NULL)
  332.     return;
  333.  
  334.   if ( OnOff ) {
  335.     SrcGadget = CycleOffGadget; /* Make it turn on */
  336.     DstGadget = CycleOnGadget;
  337.   } else {
  338.     SrcGadget = CycleOnGadget;
  339.     DstGadget = CycleOffGadget;
  340.   }
  341.  
  342.   if (SrcGadget->NextGadget != NULL) {
  343.     place = RemoveGadget( CycWind, SrcGadget );
  344.     SrcGadget->NextGadget = NULL;
  345.  
  346.     AddGadget( CycWind, DstGadget, place );
  347.  
  348.     SetAPen( CycWind->RPort, NORMALPEN );
  349.     RectFill( CycWind->RPort, SrcGadget->LeftEdge,  SrcGadget->TopEdge,
  350.                               SrcGadget->LeftEdge + SrcGadget->Width,
  351.                               SrcGadget->TopEdge +  SrcGadget->Height);
  352.     RefreshGList( DstGadget, CycWind, NULL, 1);
  353.   }
  354. }
  355.  
  356. static struct Border *PotBorder;
  357.  
  358. /*
  359.  * Allocate all the gadgets for the color palette window
  360.  */
  361. struct Gadget *MakeCycle()
  362. {
  363.            struct Gadget *FirstGadget;
  364.   register struct Gadget *NextGadget;
  365.   register LONG i,Left,x,y,c = 0;
  366.  
  367.   struct Gadget *ExcludeGadget, *PrevGadget;
  368.  
  369.   struct Image *Image0;
  370.   struct Image *Image1;
  371.   struct IntuiText *Text, *NextText;
  372.  
  373.   extern struct Image Arrow_Image[];
  374.  
  375.   LONG fourx = 4 << XScale;
  376.   LONG foury = 4 << YScale;
  377.  
  378.   char *str;
  379.  
  380.   Left = PENLEFT;
  381.  
  382.   FirstGadget = NextGadget = MakePot( 84, 35, 64, 4, CYCSPEED, y);
  383.  
  384.   if ( NextGadget == NULL ) goto error;
  385.  
  386.   SpeedPot = NextGadget;
  387.  
  388.   NextGadget->Activation = GADGIMMEDIATE | FOLLOWMOUSE | RELVERIFY;
  389.   NextGadget->GadgetText = Text = ShadowIntui("Speed",-46, -3);
  390.  
  391.   if ( Text == NULL ) goto error;
  392.  
  393.   Image0 = (struct Image *) NextGadget->GadgetRender;
  394.   Image0->Width  = 4;
  395.   Image0->Height = 4;
  396.   Image0->PlaneOnOff = NORMALPEN;
  397.  
  398.   PropInfo = (struct PropInfo *) NextGadget->SpecialInfo;
  399.   PropInfo->Flags = PROPBORDERLESS | FREEHORIZ;
  400.   PropInfo->HorizBody = 0xff;
  401.  
  402.   PotBorder = ShadowBorder( BEVELEDDOWN, 83, 34, 65, 5);
  403.  
  404.   NextGadget->NextGadget =
  405.                      MakeBool( LEFTMARG+1, 11, 48, 13, 1, CYCRANGE, NULL);
  406.  
  407.   NextGadget = NextGadget->NextGadget;
  408.  
  409.   if ( NextGadget == NULL) goto error;
  410.  
  411.   NextGadget->GadgetText = ShadowIntui("Range", 4,3);
  412.  
  413.   NextGadget->NextGadget = MakeBool( 151, 32,16, 8, 4, CYCDIR, GADGIMAGE);
  414.  
  415.   NextGadget = NextGadget->NextGadget;
  416.  
  417.   if ( NextGadget == NULL) goto error;
  418.  
  419.   DirGadget = NextGadget;
  420.  
  421.   NextGadget->Flags = GADGHIMAGE | GADGIMAGE | SELECTED;
  422.   NextGadget->Activation |= TOGGLESELECT;
  423.  
  424.   Image0 = (struct Image *) NextGadget->GadgetRender;
  425.   Image1 = (struct Image *) Image0->NextImage;
  426.   Image0->NextImage  = NULL;
  427.  
  428.   NextGadget->SelectRender = (APTR) Image1;
  429.  
  430.   SetupArrow(Image0, &Arrow_Image[0]);
  431.   SetupArrow(Image1, &Arrow_Image[1]);
  432.  
  433.   CycleOffGadget = MakeBool( 3, 28, 30, 13, 1, CYCON, NULL);
  434.  
  435.   if ( CycleOffGadget == NULL) goto error;
  436.  
  437.   CycleOffGadget->GadgetText = ShadowIntui("Off", 4,4);
  438.  
  439.   CycleOnGadget = MakeBool( 3, 28, 30, 13, 1, CYCON, NULL);
  440.  
  441.   if ( CycleOnGadget == NULL) goto error;
  442.  
  443.   CycleOnGadget->GadgetText = ShadowIntui("On", 8,4);
  444.  
  445.   if (CurPict && CurPict->CycleOn) {
  446.  
  447.     NextGadget->NextGadget = CycleOnGadget;
  448.   } else {
  449.     NextGadget->NextGadget = CycleOffGadget;
  450.   }
  451.  
  452.   NextGadget = NextGadget->NextGadget;
  453.  
  454.   for (c = 0, x = 0; c < 4; c++, x += 28) {
  455.  
  456.     NextGadget->NextGadget =
  457.       MakeBool( 55 + x, 11, 24, 13, 1, CYCRNUM+c, NULL);
  458.  
  459.     NextGadget = NextGadget->NextGadget;
  460.  
  461.     if ( NextGadget == NULL ) goto error;
  462.  
  463.     NextGadget->UserData = (APTR) PrevGadget;
  464.  
  465.     PrevGadget = NextGadget;
  466.  
  467.     NextGadget->Flags = GADGHIMAGE;
  468.     NextGadget->Activation |= TOGGLESELECT;
  469.     NextGadget->SelectRender =
  470.         (APTR) ShadowBorder( BEVELEDDOWN, 1, 1, 23, 12);
  471.  
  472.     if (c == 0) {
  473.       ExcludeGadget = NextGadget;
  474.       NextGadget->Flags |= SELECTED;
  475.       EditRange = 0;
  476.     }
  477.  
  478.     switch (c) {
  479.       case 0:  NextGadget->GadgetText = ShadowIntui("c1", 4,3); break;
  480.       case 1:  NextGadget->GadgetText = ShadowIntui("c2", 4,3); break;
  481.       case 2:  NextGadget->GadgetText = ShadowIntui("c3", 4,3); break;
  482.       case 3:  NextGadget->GadgetText = ShadowIntui("c4", 4,3);
  483.     }
  484.   }
  485.   ExcludeGadget->UserData = (APTR) NextGadget;
  486.  
  487.   return(FirstGadget);
  488.  
  489. error:
  490.   FreeGadgets( FirstGadget );
  491.   return( NULL );
  492. } /* MakeCycle */
  493.  
  494. SetupArrow( ImageDst, ImageSrc,  )
  495.   register struct Image *ImageDst;
  496.   register struct Image *ImageSrc;
  497. {
  498.   ImageDst->LeftEdge   = ImageDst->TopEdge = 0;
  499.   ImageDst->Width      = 16;
  500.   ImageDst->Height     = 9;
  501.   ImageDst->ImageData  = ImageSrc->ImageData;
  502.   ImageDst->Depth      = 1;
  503.   ImageDst->PlanePick  = NORMALPEN;
  504.   ImageDst->PlaneOnOff = 0;
  505. }
  506.  
  507. static struct Gadget *CycGadgets;
  508.  
  509. /*
  510.  * Open the Cycle window
  511.  */
  512. OpenCycWind()
  513. {
  514.   struct Window *OpenMyWind();
  515.  
  516.   register struct Gadget *gadget;
  517.   register struct RastPort *Rp;
  518.  
  519.   if (CurPict == NULL)
  520.     return;
  521.  
  522.   if ( CycWind == NULL ) {
  523.  
  524.     gadget = MakeCycle();
  525.  
  526.     if ( gadget == NULL ) {
  527.       DispErrMsg("Couldn't get palette gadgets", 0 );
  528.       return;
  529.     }
  530.  
  531.     CycWind = OpenMyWind( &NewCyc, screen, NULL, 168, 46);
  532.  
  533.     if ( CycWind != NULL ) {
  534.  
  535.       Rp = CycWind->RPort;
  536.  
  537.       SetAPen( Rp, NORMALPEN );
  538.       RectFill( Rp, LEFTMARG, TOPMARG, CycWind->Width, CycWind->Height);
  539.  
  540.       BorderWindow( CycWind );
  541.  
  542.       CycGadgets = gadget;
  543.  
  544.       AddGList( CycWind, gadget, -1, -1);
  545.  
  546.       RefreshGadgets( gadget, CycWind, NULL );
  547.       DrawBorder( Rp, PotBorder, 0L, 0L);
  548.       FreeBorder(PotBorder);
  549.     }
  550.   } else {
  551.     WindowToFront( CycWind );
  552.   }
  553.   CycleOpen = 1;
  554. } /* OpenCycWind */
  555.  
  556. /*
  557.  * Close the Cycle window
  558.  */
  559. CloseCycWind()
  560. {
  561.   register struct Gadget *FreeGadget;
  562.   if (CycWind != NULL) {
  563.  
  564.     NewCyc.LeftEdge = CycWind->LeftEdge;
  565.     NewCyc.TopEdge  = CycWind->TopEdge;
  566.  
  567.     if (CycleOffGadget->NextGadget == NULL) {
  568.       FreeGadgets( CycleOffGadget );
  569.     } else {
  570.       FreeGadgets( CycleOnGadget );
  571.     }
  572.  
  573.     CloseMyWind(CycWind,CycGadgets);
  574.     CycGadgets = NULL;
  575.   }
  576.   CycWind = NULL;
  577. } /* CloseCycWind */
  578.  
  579.